package model;

import battleship.Observer;
import battleship.Subject;
import controller.Controller;
import exceptions.IncorrectPositionException;

public class ModelFacade implements Subject, Observer
{
	private Controller controller;
	private Grid grid;
	private Ship mines;
	private Ship battle;
	private Ship destr;
	private Ship subb;
	private boolean play = true;
	
	public void positionShips()
	{
		try
		{
			this.grid = new Grid();
			this.grid.addObserver(this);
			
			this.mines = new Minesweeper();
			this.mines.addObserver(this.grid);
			this.grid.positionShip(mines);
			
			this.battle = new Battleship();
			this.battle.addObserver(this.grid);
			this.grid.positionShip(battle);
			
			this.destr = new Destroyer();
			this.destr.addObserver(this.grid);
			this.grid.positionShip(destr);
			
			this.subb = new Submarine();
			this.subb.addObserver(this.grid);
			this.grid.positionShip(subb);
			
		}catch(IncorrectPositionException e)
		{
			System.out.println(e);
		}
	}
	
	public void fire()
	{
		if( this.play == false)
			this.notifyObservers();
		else
			this.grid.fire();
	}

	@Override
	public void addObserver(Observer observer) 
	{
		this.controller = (Controller)observer;
		
	}

	@Override
	public void notifyObservers() 
	{
		this.controller.update(this, null);
	}

	@Override
	public void removeObserver(Observer observer) 
	{
		// TODO Auto-generated method stub	
	}

	@Override
	public void update(Subject subject, Object data) 
	{
		this.play = false;	
	}
}
